using System;
using System.Data;
using System.Configuration;
using System.Web;
using Fieldbook.Entities;
using System.Collections.Generic;
using Fieldbook.Common;
using Fieldbook.DAL;

namespace Fieldbook.Business
{
    public static class MessageController
    {
        
        public static List<Message> LoadUserMessages(long userID)
        {
            List<Message> res = null;
            try
            {
                string criteria = String.Format(" WHERE ReceiverID = {0} AND MessageTypeID = {1}", userID, (byte)Message.MessageType.SimpleMessage);
                res = MessageDAO.LoadMessages(criteria, "");
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting user messages", ex);
            }
            return res;
        }

        public static List<Message> LoadUserMessages(long userID, int maximumRows, int startRowIndex)
        {
            List<Message> res = null;
            try
            {
                string criteria = String.Format(" WHERE ReceiverID = {0} AND MessageTypeID = {1}", userID, (byte)Message.MessageType.SimpleMessage);
                res = MessageDAO.LoadMessages(criteria, "", maximumRows, startRowIndex + 1);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting user messages", ex);
            }
            return res;
        }

        public static Message GetMessage(long messageID)
        {
            Message res = null;
            try
            {
                string criteria = String.Format(" WHERE Messageid = {0}", messageID);
                res = MessageDAO.LoadMessages(criteria, "")[0];
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting user messages", ex);
            }
            return res;
        }

        public static int GetMessagesCount(long userID, int maximumRows, int startRowIndex)
        {
            int res = 0;
            try
            {
                string criteria = String.Format(" WHERE ReceiverID = {0} AND MessageTypeID = {1}", userID, (byte)Message.MessageType.SimpleMessage);
                res = MessageDAO.GetMessagesCount(criteria);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting user messages", ex);
            }
            return res;
        }

        public static bool UpdateMessage(Message message)
        {
            bool res = false;
            try
            {
                res = MessageDAO.UpdateMessage(message);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error getting user messages", ex);
            }
            return res;
        }
        
    }
}
